package com.timebank.volunteer.controller;

import com.timebank.common.core.utils.ip.IpUtils;
import com.timebank.common.core.web.pojo.AjaxResult;
import com.timebank.volunteer.utils.GaoDeMapUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;


/**
 * 地图控制层
 * @author shiliuyinzhen
 */
@RestController
@Api(tags = "地图相关接口")
@RequestMapping("/map")
public class GaoDeMapController {
 
    @Resource
    private GaoDeMapUtil gaoDeMapUtil;
 
    public static final Logger log = LoggerFactory.getLogger(GaoDeMapController.class);


    /**
     * 根据经纬度获取地理位置
     * @param longitude
     * @param latitude
     * @return
     */
    @ApiOperation(value = "根据经纬度获取地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "longitude", value = "经度"),
            @ApiImplicitParam(name = "latitude", value = "纬度")
    })
    @GetMapping("/getAddress")
    public AjaxResult getAddress(
            @RequestParam("longitude") String longitude,
            @RequestParam("latitude") String latitude)
    {
            return AjaxResult.success(gaoDeMapUtil.getAddressByLocation(longitude, latitude));
    }

    /**
     * 根据地理位置获取经纬度
     * @param address
     * @return
     */
    @ApiOperation(value = "根据地址获取经纬度")
    @ApiImplicitParam(name = "address", value = "地理位置")
    @GetMapping("/getLonLat")
    public  AjaxResult getLonLat(@RequestParam("address") String address) {
        return AjaxResult.success(gaoDeMapUtil.getLocationByAddress(address));
    }

    /**
     * 根据ip获取地理信息
     * @param ip
     * @return
     */
    @ApiOperation("根据ip获取地理信息")
    @GetMapping("/ip")
    public AjaxResult getPositionByIp(@RequestParam("ip") String ip){
        if (ip == null || ip == ""){
            ip = IpUtils.getIp();
        }
        return gaoDeMapUtil.getPositionByIp(ip);
    }

    /**
     * 根据两个定位点的经纬度算出两点间的距离(单位：米)
     * @param startLonLat
     * @param endLonLat
     * @return
     */
    @ApiOperation(value = "根据两个定位点的经纬度算出两点间的距离(单位：米)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startLonLat", value = "起始经纬度"),
            @ApiImplicitParam(name = "endLonLat", value = "结束经纬度")
    })
    @GetMapping("/getDistance")
    public AjaxResult getDistance(
            @RequestParam("startLonLat") String startLonLat,
            @RequestParam("endLonLat") String endLonLat) {
        Long distance = gaoDeMapUtil.getDistance(startLonLat, endLonLat);
        HashMap<String, Object> map = new HashMap<>();
        map.put("startLonLat",startLonLat);
        map.put("endLonLat",endLonLat);
        map.put("distance",distance);
        return AjaxResult.success(map);
    }

}